package com.chinamcom.framework.admin.common.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.chinamcom.framework.admin.common.model.${className};
import com.chinamcom.framework.admin.common.service.${className}Service;

/**
 * 
 * @author fattiger.xiaoyang
 * @date  ${.now?string("yyyy/MM/dd")}
 */

@Controller
@RequestMapping("${tableName}")
public class ${className}Controller {
	
	@Autowired
	private ${className}Service ${className?uncap_first}Service;
	
	@RequestMapping("list.do")
	public String getList(Integer page, Integer pageSize, ModelMap map){
		if(page == null || page < 0){
			page = 0;
		}
		if(pageSize == null || pageSize < 0 || pageSize > 100){
			pageSize = 10;
		}
		Page<${className}> pageList = ${className?uncap_first}Service.list(page, pageSize);
		map.put("lists", 		pageList.getContent());
    	map.put("totalPageNum", pageList.getTotalPages());
    	map.put("currentPage", 	pageList.getNumber());
    	map.put("totalNum", 	pageList.getTotalElements());
		return "${className?uncap_first}/${className?uncap_first}_list";
	}
	
	@RequestMapping(value = "save.do", method = RequestMethod.POST)
	public String save(${className} entity){
		${className?uncap_first}Service.save(entity);
		return "redirect:list.do";
	}
	
	@RequestMapping(value = "edit.do", method = RequestMethod.GET)
	public String edit(Long id, Integer page, ModelMap map){
		${className} entity = null;
		if(id != null){
			entity = ${className?uncap_first}Service.get(id);
		}
		map.put("entity", entity);
		map.put("page", page);
		return "${className?uncap_first}/${className?uncap_first}_edit";
	}

}
